package study.method.polymorphism;

/**
 * @author Administrator
 * @date 2020/12/20
 */
public class Application {
    public static void main(String[] args) {
        Animal a1 = new Cat();
//        Cat cat = ((Cat) a1);
//        cat.eat();  //输出我吃鱼
//        Dog dog = ((Dog) a1);
        //返回报错:java.lang.ClassCastException: Study.Method.Base.Polymorphism.Cat cannot be cast to Study.Method.Base.Polymorphism.Dog
        //a1为Cat对象,理所当然不能突然变成Dog对象
//        dog.eat();
//        Animal a2 = new Animal();
//        Cat cat1 = ((Cat) a2);
        //返回报错:java.lang.ClassCastException: Study.Method.Base.Polymorphism.Animal cannot be cast to Study.Method.Base.Polymorphism.Cat
        //a2为Animal对象,无法使用子类中的方法
//        cat1.eat();

        /*
        instanceof详解
        Object > String
        Object > Animal > Cat
        Object > Animal > Dog
         */
        Object object = new Cat();
        System.out.println(object instanceof Object);   //true
        System.out.println(object instanceof Animal);   //true
        System.out.println(object instanceof Cat);      //true
        System.out.println(object instanceof Dog);      //false
        System.out.println(object instanceof String);   //false
        System.out.println("======================");
        Animal animal = new Cat();
        System.out.println(animal instanceof Object);   //true
        System.out.println(animal instanceof Animal);   //true
        System.out.println(animal instanceof Cat);      //true
        System.out.println(animal instanceof Dog);      //false
//        System.out.println(animal instanceof String);   //编译报错,因为Animal与String同级,没有继承关系
        System.out.println("======================");
        Cat cat = new Cat();
        System.out.println(cat instanceof Object);      //true
        System.out.println(cat instanceof Animal);      //true
        System.out.println(cat instanceof Cat);         //true
//        System.out.println(cat instanceof Dog);         //编译错误,因为Cat与Dog同级,没有继承关系
//        System.out.println(cat instanceof String);      //编译错误,因为Cat与String没有任何关系
        System.out.println("======================");
        System.out.println(null instanceof Object);
    }
}
